<template>
  <div class="scroll-view-container">
    <!-- 左边的一级分类 -->
    <div class="left-scroll-view">
      <!-- <div
        class="left-scroll-view-item"
        v-for="(item, index) in categories"
        :key="item.cat_id"
        :class="{ active: index === currentIndex }"
        @click="currentIndex = index"
      > -->
      <!-- 样式绑定有两种写法，上面是对象的写法，如果 index === currentIndex 就加上active 样式 -->
      <div
        :class="[
          'left-scroll-view-item',
          index === currentIndex ? 'active' : ''
        ]"
        v-for="(item, index) in categories"
        :key="item.cat_id"
        @click="currentIndex = index"
      >
        <!-- 样式绑定有两种写法，上面是数组的写法，如果 index === currentIndex 就加上active 样式 -->
        {{ item.cat_name }}
      </div>
    </div>
    <!-- 右边的二级和三级 -->
    <div class="right-scroll-view" v-if="level2List?.length > 0">
      <div v-for="item in level2List" :key="item.cat_id" class="cate-lv2-title">
        <h4>{{ item.cat_name }}</h4>
        <!-- 遍历该二级下的三级分类 -->
        <div class="cate-lv3-list">
          <!-- 三级的分类 -->
          <div
            class="cate-lv3-item"
            v-for="subitem in item.children"
            :key="subitem.cat_id"
          >
            <img :src="subitem.cat_icon" />
            <span class="name">{{ subitem.cat_name }}</span>
          </div>
        </div>
      </div>
    </div>
    <van-empty v-else image-size="10rem" description="暂无二级分类~" />
  </div>
</template>

<script setup>
import { ref, watchEffect } from 'vue'
import { getCategories } from '@/api/category'
const currentIndex = ref(0)
const categories = ref([])
const level2List = ref([])

const getCategoriesData = async () => {
  const res = await getCategories()
  categories.value = res.data
  level2List.value = res.data[currentIndex.value].children
}

watchEffect(() => {
  level2List.value = categories.value[currentIndex.value]?.children
})

getCategoriesData()
</script>

<style lang="scss" scoped>
.scroll-view-container {
  display: flex;
  height: 100%;

  .left-scroll-view {
    width: 120px;
    height: calc(100vh - 52px);
    overflow: auto;

    .left-scroll-view-item {
      line-height: 60px;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 12px;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;
        color: #c00000;
      }
    }
  }

  // 去除滚动条
  .left-scroll-view::-webkit-scrollbar {
    width: 0 !important;
  }

  .right-scroll-view {
    height: calc(100vh - 52px);
    overflow: auto;
    flex: 1;
    .cate-lv2-title {
      font-size: 12px;
      font-weight: bold;
      padding: 0 15px;
    }
    .cate-lv3-list {
      display: flex;
      flex-wrap: wrap;

      .cate-lv3-item {
        width: 33.33%;
        margin-bottom: 10px;
        display: flex;
        flex-direction: column;
        align-items: center;

        img {
          width: 60px;
          height: 60px;
        }

        .name {
          font-size: 12px;
        }
      }
    }
  }

  // 去除滚动条
  .right-scroll-view::-webkit-scrollbar {
    width: 0 !important;
  }
}
</style>
